package com.example.fechat.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Adapter
import com.bumptech.glide.Glide
import com.example.fechat.R
import com.example.fechat.base.BaseApplication
import com.example.fechat.bean.MessageBean
import com.example.fechat.room.user.UserEntity
import com.example.fechat.utils.Files
import com.example.fechat.view.CircleImageView
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.util.SmartGlideImageLoader
import java.text.SimpleDateFormat
import java.util.*


class ChatAdapter(
    private val context: Context,
    private val data: List<MessageBean>,
    private val uris: List<String>,
    private val userEntity: UserEntity
) :
    Adapter<ChatAdapter.BaseHolder>() {

    private fun getTime(time: Long): String {
        val sDateFormat = SimpleDateFormat("MM-dd HH:mm", Locale.getDefault())
        return sDateFormat.format(time)
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setNewData() {
        notifyDataSetChanged()
    }

    class BaseHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val userLayout: RelativeLayout = itemView.findViewById(com.example.fechat.R.id.userLayout)
        val userHead: CircleImageView = itemView.findViewById(com.example.fechat.R.id.userHead)
        val userMessage: TextView = itemView.findViewById(com.example.fechat.R.id.userMessage)
        val userTime: TextView = itemView.findViewById(com.example.fechat.R.id.userTime)
        val userImage: ImageView = itemView.findViewById(com.example.fechat.R.id.userImage)

        val respLayout: RelativeLayout = itemView.findViewById(com.example.fechat.R.id.respLayout)
        val respHead: CircleImageView = itemView.findViewById(com.example.fechat.R.id.respHead)
        val respMessage: TextView = itemView.findViewById(com.example.fechat.R.id.respMessage)
        val respTime: TextView = itemView.findViewById(com.example.fechat.R.id.respTime)
        val respImage: ImageView = itemView.findViewById(com.example.fechat.R.id.respImage)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_message, null, false);
        return BaseHolder(view)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: BaseHolder, position: Int) {
        val item = data[position]
        val imagePosition = uris.indexOf(Files.Uri2Path(context, Uri.parse(item.message)))
        if (item.isResponse) {
            holder.respLayout.visibility = View.VISIBLE
            holder.userLayout.visibility = View.GONE
            if (item.message.contains("content://")) {
                holder.respMessage.visibility = View.GONE
                holder.respImage.visibility = View.VISIBLE
                val imagePath =
                    Files.Uri2Path(context, Uri.parse(item.message))
                Glide.with(context)
                    .load(imagePath)
                    .into(holder.respImage)
                holder.respImage.setOnClickListener {
                    showImageView(
                        holder.userImage,
                        imagePosition
                    )
                }
            } else {
                holder.respMessage.visibility = View.VISIBLE
                holder.respImage.visibility = View.GONE
                holder.respMessage.text = item.message
            }
            holder.respTime.text = getTime(item.time)

            Glide.with(context)
                .load(BaseApplication.headers?.get(userEntity.userHead.toInt()))
                .into(holder.respHead)
        } else {
            holder.userLayout.visibility = View.VISIBLE
            holder.respLayout.visibility = View.GONE

            if (item.message.contains("content://")) {
                holder.userMessage.visibility = View.GONE
                holder.userImage.visibility = View.VISIBLE
                val imagePath =
                    Files.Uri2Path(context, Uri.parse(item.message))
                Glide.with(context)
                    .load(imagePath)
                    .into(holder.userImage)
                holder.userImage.setOnClickListener {
                    showImageView(
                        holder.userImage,
                        imagePosition
                    )
                }
            } else {
                holder.userMessage.visibility = View.VISIBLE
                holder.userImage.visibility = View.GONE
                holder.userMessage.text = item.message
            }
            holder.userTime.text = getTime(item.time)
        }
    }

    private fun showImageView(imageView: ImageView, currentPosition: Int) {
        XPopup.Builder(context)
            .isDestroyOnDismiss(true)
            .asImageViewer(
                imageView, currentPosition, uris,
                { popupView, position ->
                    /*Toast.makeText(
                        context, "切换到第" + position + "个图片", Toast.LENGTH_SHORT
                    ).show()*/
                },
                SmartGlideImageLoader(R.mipmap.ic_launcher)
            )
            .show()
    }
}